var showUnkownUserByRole, 
	  saveUserRole;
var id, 
	  corpId;
require([ 'grid'], function () {
	var $addRoleList = $("#addRoleList");
	$addRoleList.grid({
		url : "/sysUser/getUnkownRoleListByUserRoleVo.htm",
		width:495,
		height:200,
		colModel:[
			{name: "id", index: "id", key: true, label:"id", hidden: true},
			{name: "name", index: "name", label:"角色名称", sorttype: "text",align: "center",width: 220},
			{name: "remark", index: "remark", label:"备注", sorttype: "text",align: "center",width: 220}
		]
	});
	
	/**
	 * 加载用户为分配角色列表
	 * @param _id 用户id
	 * @param _name 查询关键字
	 * @param _corpId 所属公司id
	 */
	 showUnkownUserByRole = function(_id, _name, _corpId){
		 id = _id;
		 corpId =  _corpId;
		$addRoleList.grid('setGridParam', {page:1,datatype:"json",postData:{"userId":_id, "name":_name, "corpId":_corpId}}).trigger("reloadGrid");
	}
	
	//弹出对话框的用户名搜索
	$("#searchNameBtn").click(function(){
		showUnkownUserByRole(id, $("#searchName").val(), corpId);
	});
	
	/**
	 * 保存动作
	 * @param userId 用户id
	 * @param corpId 公司id
	 * @param $roleList 用户角色关系列表
	 */
	saveUserRole = function(userId, corpId, $roleList){
		var roleIds = $addRoleList.grid('getGridParam','selarrrow');
		$.ajaxExt({
			url:"/sysUser/saveUserRole.htm",
			data:{"roleIds":roleIds,"userId":userId}
		}).done(function(){
			$.messager.alert("提示","保存成功!","info");
			$roleList.grid('setGridParam', {
	            page: 1,
	            datatype: "json",
	            postData: {userId: userId, corpId: corpId}
	        }).trigger("reloadGrid");
		});
	}
});